home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / sbp.zoo / sbp_v3.1 / auxil / addbfcrc.c next >
C/C++ Source or Header  |  1992-02-16  |  1KB  |  62 lines

  1. /* adbfcrc.c */
  2.  
  3. /*
  4. addbfcrc() accepts a buffer address and a count and adds the CRC for
  5. all bytes in the buffer to the global variable crccode using
  6. CRC-16.
  7.  
  8. This file is public domain.
  9.  
  10.                                     -- Rahul Dhesi 1991/07/07
  11. */
  12.  
  13. #include "booz.h"
  14.  
  15. #define TABLEN    256
  16.  
  17. unsigned int crccode;
  18. unsigned int crctab[TABLEN];
  19.  
  20. int addbfcrc(char *buffer, unsigned count)
  21.  
  22. {
  23.    register unsigned int localcrc;
  24.    register int i;
  25.    localcrc = crccode;
  26.  
  27.    for (i=0; i<count; i++)
  28.       localcrc = (localcrc>>8) ^ crctab[(localcrc ^ (buffer[i])) & 0x00ff];
  29.    crccode = localcrc;
  30. }
  31.  
  32. /* gentab() generates table for CRC calculation, as described in
  33. "C Programmer's Guide to Serial Communications" by Joe Campbell */
  34.  
  35. /* reverse CRC-16 polynomial */
  36. #define CRC_FUNC        (unsigned) 0xa001
  37.  
  38. unsigned int calcterm();
  39.  
  40. unsigned int calcterm (register unsigned int data)
  41. {
  42.    int i;
  43.    register unsigned int accum = 0;
  44.  
  45.    data <<= 1;
  46.    for (i = 8;  i > 0;  i--) {
  47.       data >>= 1;
  48.       if ((data ^ accum) & 0x0001)
  49.          accum = (accum >> 1) ^ CRC_FUNC;
  50.       else
  51.          accum >>= 1;
  52.    }
  53.    return accum;
  54. }
  55.  
  56. int gentab(void)
  57. {
  58.    register unsigned int i;
  59.    for (i = 0;  i < TABLEN;  i++)
  60.       crctab[i] = calcterm (i);
  61. }
  62.